package com.rentong.web.controller.system;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.rentong.common.exception.ServiceException;
import com.rentong.common.utils.StringUtils;
import com.rentong.cust.domain.CustUser;
import com.rentong.cust.mapper.CustUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rentong.common.constant.Constants;
import com.rentong.common.core.domain.AjaxResult;
import com.rentong.common.core.domain.entity.SysMenu;
import com.rentong.common.core.domain.entity.SysUser;
import com.rentong.common.core.domain.model.LoginBody;
import com.rentong.common.core.domain.model.LoginUser;
import com.rentong.common.utils.SecurityUtils;
import com.rentong.framework.web.service.SysLoginService;
import com.rentong.framework.web.service.SysPermissionService;
import com.rentong.framework.web.service.TokenService;
import com.rentong.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CustUserMapper custUserMapper;

    @Autowired
    private SysLoginService sysLoginService;




    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        //验证用户名密码
        CustUser user = custUserMapper.getUserByPhone(loginBody.getUsername());
        if (user == null || !StringUtils.equals(loginBody.getPassword(), user.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        LoginUser loginUser = new LoginUser(user.getCustId(), user.getDeptId(), user, null); //自己组装需要的LoginUser
        sysLoginService.recordLoginInfo(loginUser.getUserId());
        String token = tokenService.createToken(loginUser);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合

        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Set<String> perms = new HashSet<String>();
        perms.add("*:*:*");
        loginUser.setPermissions(perms);
        if (!loginUser.getPermissions().equals(permissions)) {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters(@CookieValue("username") String username) {
        CustUser user = custUserMapper.getUserByPhone(username);
//        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getCustId(),user.getGradeType());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
